package com.ruoyi.web.controller.construction.strategy;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.ConstructionVideo;
import com.ruoyi.system.domain.construction.StrategyVideo;
import com.ruoyi.system.domain.construction.StrategyVr;
import com.ruoyi.system.domain.construction.strategy.ConstructionStrategy;
import com.ruoyi.system.service.construction.StrategyService;
import com.ruoyi.system.service.construction.StrategyVideoService;
import com.ruoyi.system.service.construction.StrategyVrService;
import com.ruoyi.system.service.construction.strategy.ConstructionStrategyService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 施工攻略Controller
 *
 * @author zhangshaocong
 * @date 2020-12-03
 */
@RestController
@RequestMapping("/construction/strategy")
public class ConstructionStrategyController extends BaseController
{
    @Autowired
    private ConstructionStrategyService constructionStrategyService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private StrategyVideoService strategyVideoService;

    @Autowired
    private StrategyVrService strategyVrService;

    /**
     * 查询施工攻略列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ConstructionStrategy constructionStrategy)
    {
        startPage();
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        constructionStrategy.setUserId(userId);
        List<ConstructionStrategy> list = constructionStrategyService.selectConstructionStrategyList(constructionStrategy);
        return getDataTable(list);
    }

    /**
     * 导出施工攻略列表
     */

    @Log(title = "施工攻略", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ConstructionStrategy constructionStrategy)
    {
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<ConstructionStrategy> list = constructionStrategyService.selectConstructionStrategyList(constructionStrategy);
        ExcelUtil<ConstructionStrategy> util = new ExcelUtil<ConstructionStrategy>(ConstructionStrategy.class);
        return util.exportExcel(list, "strategy");
    }

    /**
     * 获取施工攻略详细信息
     */

    @GetMapping(value = "/{constructionStrategyId}")
    public AjaxResult getInfo(@PathVariable("constructionStrategyId") Long constructionStrategyId)
    {
        return AjaxResult.success(constructionStrategyService.selectConstructionStrategyById(constructionStrategyId));
    }

    /**
     * 新增施工攻略
     */

    @Log(title = "施工攻略", businessType = BusinessType.INSERT)
    @PostMapping(value = "/INSERTa")
    public AjaxResult add(@RequestBody ConstructionStrategy constructionStrategy)
    {
        System.out.println("**********************************************************"+constructionStrategy);
        constructionStrategy.setConstructionStrategyTime(new Date());
        constructionStrategy.setModifiedTime(new Date());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        constructionStrategy.setUserId(userId);
        return toAjax(constructionStrategyService.insertConstructionStrategy(constructionStrategy));
    }

    /**
     * 修改施工攻略
     */

    @Log(title = "施工攻略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstructionStrategy constructionStrategy)
    {
        return toAjax(constructionStrategyService.updateConstructionStrategy(constructionStrategy));
    }

    /**
     * 删除施工攻略
     */

    @Log(title = "施工攻略", businessType = BusinessType.DELETE)
	@DeleteMapping("/{constructionStrategyIds}")
    public AjaxResult remove(@PathVariable Long[] constructionStrategyIds)
    {
        return toAjax(constructionStrategyService.deleteConstructionStrategyByIds(constructionStrategyIds));
    }

    /**
     * 查询店铺信息列表
     * @return
     */

    @GetMapping(value = "/selectSupplierList")
    public AjaxResult selectSupplierList(){
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        return AjaxResult.success(constructionStrategyService.selectSupplierList(userId));
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public Map<String,Object> up(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai/";
        return FileUtil.uploadImage(file,url);
    }



//    @PostMapping("/uploadVideo")
//    public AjaxResult uploadVideo(@RequestParam("videoFile") MultipartFile file,@RequestParam("constructionStrategyId") Long constructionStrategyId) throws IOException {
//        System.out.println("constructionStrategyId:"+constructionStrategyId);
//        if (!file.isEmpty())
//        {
//            String videoPath = RuoYiConfig.getVideoPath();
//            String video = FileUploadUtils.uploadVideo(videoPath, file);
//            StrategyVideo strategyVideo = new StrategyVideo();
//            strategyVideo.setConstructionStrategyId(constructionStrategyId);
//            strategyVideo.setVidUrl("http://39.98.237.139"+video);
//            strategyVideoService.insertStrategyVideo(strategyVideo);
//        }
//        return AjaxResult.error("上传视频异常，请联系管理员");
//    }

    @PostMapping("/uploadVideo")
    public Map<String, Object> uploadVideo(@RequestParam("videoFile") MultipartFile file) throws IOException {
        String videoPath = RuoYiConfig.getVideoPath();
        String video = FileUploadUtils.uploadVideo(videoPath, file);
        return FileUtil.uploadImage(file,video);
    }



    ///******************以下是VR的东西******************
    @GetMapping("/listvr")
    public TableDataInfo list(StrategyVr strategyVr)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        strategyVr.setUserId(userId);
        List<StrategyVr> list = strategyVrService.selectStrategyVrList(strategyVr);
        return getDataTable(list);
    }
    @PostMapping(value = "/addvr")
    public AjaxResult add(@RequestBody StrategyVr strategyVr)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        strategyVr.setUserId(userId);
        Long constructionStrategyId = strategyVr.getConstructionStrategyId();
        strategyVr.setConstructionStrategyId(constructionStrategyId);
        strategyVr.setState("1");
        return toAjax(strategyVrService.insertStrategyVr(strategyVr));
    }

    @PostMapping("/uploadImageVr")
    public Map<String,Object> upVr(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai/";
        return FileUtil.uploadImage(file,url);
    }


    /**
     * 视频上传
     */
    @Log(title = "视频上传", businessType = BusinessType.UPDATE)
    @PostMapping("/upVideo")
    public AjaxResult upVideo(@RequestParam("carouselFile") MultipartFile file) throws IOException {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getVideo(), file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", "http://51jhf.com/"+avatar);
            return ajax;
        }
        return AjaxResult.error("上传视频异常，请联系管理员");
    }


}
